package com.wry.wry_test.controller;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wangrongyi
 * @Date: 2024/7/16 15:55
 * @Description:
 */
@RestController
@RequestMapping("/cache")
@Slf4j
public class CacheController {


    @Autowired
    private Cache<String, Object> cache;

    @GetMapping("/getCacheInfo")
    public void getCacheInfo() throws InterruptedException {
        String s = (String) cache.getIfPresent("key");
        if (s == null) {
            cache.put("key", "value");
            Thread.sleep(3000);
        }
        log.info("cache:{}", cache.getIfPresent("key"));
    }


    @GetMapping("/getCache")
    @Cacheable(value = "cache", key = "'key'")
    public String getCache() throws InterruptedException {

        Thread.sleep(3000);
        return "getCache";
    }


    @GetMapping("/setCache")
    @CachePut(value = "cache", key = "'key'")
    public String setCache() throws InterruptedException {
        Thread.sleep(3000);
        return "setCache";
    }


    @GetMapping("/delCache")
    @CacheEvict(value = "cache", key = "'key'")
    public String delCache() throws InterruptedException {
        Thread.sleep(3000);
        return "delCache";
    }


}
